

<!DOCTYPE html>
<html class="writer-html5" lang="en" >
<head>
  <meta charset="utf-8" />
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  
  <title>mindspore.Tensor &mdash; MindSpore master documentation</title>
  

  
  <link rel="stylesheet" href="../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />

  
  

  
  

  

  
  <!--[if lt IE 9]>
    <script src="../../_static/js/html5shiv.min.js"></script>
  <![endif]-->
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../../" src="../../_static/documentation_options.js"></script>
        <script src="../../_static/jquery.js"></script>
        <script src="../../_static/underscore.js"></script>
        <script src="../../_static/doctools.js"></script>
        <script src="../../_static/language_data.js"></script>
        <script async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
    
    <script type="text/javascript" src="../../_static/js/theme.js"></script>

    
    <link rel="index" title="Index" href="../../genindex.html" />
    <link rel="search" title="Search" href="../../search.html" />
    <link rel="next" title="mindspore.RowTensor" href="mindspore.RowTensor.html" />
    <link rel="prev" title="mindspore" href="../mindspore.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="../../index.html" class="icon icon-home"> MindSpore
          

          
          </a>

          
            
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        
        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <p class="caption"><span class="caption-text">MindSpore Python API</span></p>
<ul class="current">
<li class="toctree-l1 current"><a class="reference internal" href="../mindspore.html">mindspore</a><ul class="current">
<li class="toctree-l2 current"><a class="reference internal" href="../mindspore.html#id1">张量</a><ul class="current">
<li class="toctree-l3 current"><a class="current reference internal" href="#">mindspore.Tensor</a></li>
<li class="toctree-l3"><a class="reference internal" href="mindspore.RowTensor.html">mindspore.RowTensor</a></li>
<li class="toctree-l3"><a class="reference internal" href="mindspore.SparseTensor.html">mindspore.SparseTensor</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="../mindspore.html#id2">参数</a></li>
<li class="toctree-l2"><a class="reference internal" href="../mindspore.html#id3">数据类型</a></li>
<li class="toctree-l2"><a class="reference internal" href="../mindspore.html#id4">随机种子</a></li>
<li class="toctree-l2"><a class="reference internal" href="../mindspore.html#id5">模型</a></li>
<li class="toctree-l2"><a class="reference internal" href="../mindspore.html#id6">数据处理工具</a></li>
<li class="toctree-l2"><a class="reference internal" href="../mindspore.html#id7">混合精度管理</a></li>
<li class="toctree-l2"><a class="reference internal" href="../mindspore.html#id8">序列化</a></li>
<li class="toctree-l2"><a class="reference internal" href="../mindspore.html#id9">即时编译</a></li>
<li class="toctree-l2"><a class="reference internal" href="../mindspore.html#id10">日志</a></li>
<li class="toctree-l2"><a class="reference internal" href="../mindspore.html#id11">自动混合精度</a></li>
<li class="toctree-l2"><a class="reference internal" href="../mindspore.html#id12">安装验证</a></li>
<li class="toctree-l2"><a class="reference internal" href="../mindspore.html#id13">调试</a></li>
<li class="toctree-l2"><a class="reference internal" href="../mindspore.html#id14">内存回收</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.common.initializer.html">mindspore.common.initializer</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.communication.html">mindspore.communication</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.compression.html">mindspore.compression</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.context.html">mindspore.context</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.dataset.html">mindspore.dataset</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.dataset.audio.html">mindspore.dataset.audio</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.dataset.config.html">mindspore.dataset.config</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.dataset.text.html">mindspore.dataset.text</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.dataset.transforms.html">mindspore.dataset.transforms</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.dataset.vision.html">mindspore.dataset.vision</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.mindrecord.html">mindspore.mindrecord</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.nn.html">mindspore.nn</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.nn.probability.html">mindspore.nn.probability</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.nn.transformer.html">mindspore.nn.transformer</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.numpy.html">mindspore.numpy</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.ops.html">mindspore.ops</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.parallel.html">mindspore.parallel</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.parallel.nn.html">mindspore.parallel.nn</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.profiler.html">mindspore.profiler</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.scipy.html">mindspore.scipy</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.train.html">mindspore.train</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.boost.html">mindspore.boost</a></li>
</ul>
<p class="caption"><span class="caption-text">MindSpore C++ API</span></p>
<ul>
<li class="toctree-l1"><a class="reference external" href="https://www.mindspore.cn/lite/api/zh-CN/master/api_cpp/mindspore.html">MindSpore Lite↗</a></li>
</ul>

            
          
        </div>
        
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../index.html">MindSpore</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          

















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../index.html" class="icon icon-home"></a> &raquo;</li>
        
          <li><a href="../mindspore.html">mindspore</a> &raquo;</li>
        
      <li>mindspore.Tensor</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
          
            <a href="../../_sources/api_python/mindspore/mindspore.Tensor.rst.txt" rel="nofollow"> View page source</a>
          
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <div class="section" id="mindspore-tensor">
<h1>mindspore.Tensor<a class="headerlink" href="#mindspore-tensor" title="Permalink to this headline">¶</a></h1>
<dl class="class">
<dt id="mindspore.Tensor">
<em class="property">class </em><code class="sig-prename descclassname">mindspore.</code><code class="sig-name descname">Tensor</code><span class="sig-paren">(</span><em class="sig-param">input_data=None</em>, <em class="sig-param">dtype=None</em>, <em class="sig-param">shape=None</em>, <em class="sig-param">init=None</em>, <em class="sig-param">internal=False</em><span class="sig-paren">)</span><a class="headerlink" href="#mindspore.Tensor" title="Permalink to this definition">¶</a></dt>
<dd><p>张量，即存储多维数组（n-dimensional array）的数据结构。</p>
<p><strong>参数：</strong></p>
<ul class="simple">
<li><p><strong>input_data</strong> (Union[Tensor, float, int, bool, tuple, list, numpy.ndarray]) - 被存储的数据，可以是其它Tensor，也可以是Python基本数据（如int，float，bool等），或是一个NumPy对象。默认值：None。</p></li>
<li><p><strong>dtype</strong> (<a class="reference internal" href="mindspore.dtype.html#mindspore.dtype" title="mindspore.dtype"><code class="xref py py-class docutils literal notranslate"><span class="pre">mindspore.dtype</span></code></a>) - 用于定义该Tensor的数据类型，必须是 <em>mindSpore.dtype</em> 中定义的类型。如果该参数为None，则数据类型与 <cite>input_data</cite> 一致，默认值：None。</p></li>
<li><p><strong>shape</strong> (Union[tuple, list, int]) - 用于定义该Tensor的形状。如果指定了 <cite>input_data</cite> ，则无需设置该参数。默认值：None。</p></li>
<li><p><strong>init</strong> (Initializer) - 用于在并行模式中延迟Tensor的数据的初始化，如果指定该参数，则 <cite>dtype</cite> 和 <cite>shape</cite> 也必须被指定。不推荐在非自动并行之外的场景下使用该接口。只有当调用 <cite>Tensor.init_data</cite> 时，才会使用指定的 <cite>init</cite> 来初始化Tensor数据。默认值：None。</p></li>
</ul>
<p><strong>输出：</strong></p>
<p>Tensor。</p>
<p><strong>样例：</strong></p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">mindspore</span> <span class="k">as</span> <span class="nn">ms</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">mindspore</span> <span class="kn">import</span> <span class="n">Tensor</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">mindspore.common.initializer</span> <span class="kn">import</span> <span class="n">One</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1"># initialize a tensor with numpy.ndarray</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">t1</span> <span class="o">=</span> <span class="n">Tensor</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">]),</span> <span class="n">ms</span><span class="o">.</span><span class="n">float32</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">t1</span><span class="p">)</span>
<span class="go">[[[0. 0. 0.]</span>
<span class="go">[0. 0. 0.]]]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="nb">type</span><span class="p">(</span><span class="n">t1</span><span class="p">))</span>
<span class="go">&lt;class &#39;mindspore.common.tensor.Tensor&#39;&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">t1</span><span class="o">.</span><span class="n">shape</span><span class="p">)</span>
<span class="go">(1, 2, 3)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">t1</span><span class="o">.</span><span class="n">dtype</span><span class="p">)</span>
<span class="go">Float32</span>
<span class="go">&gt;&gt;&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1"># initialize a tensor with a float scalar</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">t2</span> <span class="o">=</span> <span class="n">Tensor</span><span class="p">(</span><span class="mf">0.1</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">t2</span><span class="p">)</span>
<span class="go">0.1</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="nb">type</span><span class="p">(</span><span class="n">t2</span><span class="p">))</span>
<span class="go">&lt;class &#39;mindspore.common.tensor.Tensor&#39;&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">t2</span><span class="o">.</span><span class="n">shape</span><span class="p">)</span>
<span class="go">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">t2</span><span class="o">.</span><span class="n">dtype</span><span class="p">)</span>
<span class="go">Float32</span>
<span class="go">&gt;&gt;&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1"># initialize a tensor with a tuple</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">t3</span> <span class="o">=</span> <span class="n">Tensor</span><span class="p">((</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">t3</span><span class="p">)</span>
<span class="go">[1 2]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="nb">type</span><span class="p">(</span><span class="n">t3</span><span class="p">))</span>
<span class="go">&lt;class &#39;mindspore.common.tensor.Tensor&#39;&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">t3</span><span class="o">.</span><span class="n">shape</span><span class="p">)</span>
<span class="go">(2,)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">t3</span><span class="o">.</span><span class="n">dtype</span><span class="p">)</span>
<span class="go">Int64</span>
<span class="gp">...</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1"># initialize a tensor with init</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">t4</span> <span class="o">=</span> <span class="n">Tensor</span><span class="p">(</span><span class="n">shape</span> <span class="o">=</span> <span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">3</span><span class="p">),</span> <span class="n">dtype</span><span class="o">=</span><span class="n">ms</span><span class="o">.</span><span class="n">float32</span><span class="p">,</span> <span class="n">init</span><span class="o">=</span><span class="n">One</span><span class="p">())</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">t4</span><span class="p">)</span>
<span class="go">[[1. 1. 1.]]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="nb">type</span><span class="p">(</span><span class="n">t4</span><span class="p">))</span>
<span class="go">&lt;class &#39;mindspore.common.tensor.Tensor&#39;&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">t4</span><span class="o">.</span><span class="n">shape</span><span class="p">)</span>
<span class="go">(1, 3)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">t4</span><span class="o">.</span><span class="n">dtype</span><span class="p">)</span>
<span class="go">Float32</span>
</pre></div>
</div>
<dl class="method">
<dt id="mindspore.Tensor.T">
<em class="property">property </em><code class="sig-name descname">T</code><a class="headerlink" href="#mindspore.Tensor.T" title="Permalink to this definition">¶</a></dt>
<dd><p>返回转置后的Tensor。</p>
</dd></dl>

<dl class="method">
<dt id="mindspore.Tensor.abs">
<code class="sig-name descname">abs</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#mindspore.Tensor.abs" title="Permalink to this definition">¶</a></dt>
<dd><p>返回每个元素的绝对值。</p>
<p><strong>返回：</strong></p>
<p>Tensor。</p>
<p><strong>支持平台：</strong></p>
<p><code class="docutils literal notranslate"><span class="pre">Ascend</span></code> <code class="docutils literal notranslate"><span class="pre">GPU</span></code> <code class="docutils literal notranslate"><span class="pre">CPU</span></code></p>
<p><strong>样例：</strong></p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">mindspore</span> <span class="kn">import</span> <span class="n">Tensor</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="n">Tensor</span><span class="p">([</span><span class="mf">1.1</span><span class="p">,</span> <span class="o">-</span><span class="mf">2.1</span><span class="p">])</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="s2">&quot;float32&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">output</span> <span class="o">=</span> <span class="n">a</span><span class="o">.</span><span class="n">abs</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">output</span><span class="p">)</span>
<span class="go">[1.1 2.1]</span>
</pre></div>
</div>
</dd></dl>

<dl class="method">
<dt id="mindspore.Tensor.all">
<code class="sig-name descname">all</code><span class="sig-paren">(</span><em class="sig-param">axis=()</em>, <em class="sig-param">keep_dims=False</em><span class="sig-paren">)</span><a class="headerlink" href="#mindspore.Tensor.all" title="Permalink to this definition">¶</a></dt>
<dd><p>检查在指定轴上所有元素是否均为True。</p>
<p><strong>参数：</strong></p>
<ul class="simple">
<li><p><strong>axis</strong> (Union[None, int, tuple(int)) - 计算all的维度。 当 <cite>axis</cite> 为None或者空元组的时候，计算所有维度。当 <cite>axis</cite> 为int或tuple(int)时，记Tensor的维度为dim，则其取值范围为[-dim, dim)。默认值：()。</p></li>
<li><p><strong>keep_dims</strong> (bool) - 计算结果是否保留维度。 默认值： False。</p></li>
</ul>
<p><strong>返回：</strong></p>
<p>Tensor。如果在指定轴方向上所有数组元素都为True，则其值为True，否则其值为False。如果轴为None或空元组，则默认降维。</p>
<p><strong>支持平台：</strong></p>
<p><code class="docutils literal notranslate"><span class="pre">Ascend</span></code> <code class="docutils literal notranslate"><span class="pre">GPU</span></code> <code class="docutils literal notranslate"><span class="pre">CPU</span></code></p>
<p><strong>样例：</strong></p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">mindspore</span> <span class="kn">import</span> <span class="n">Tensor</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="n">Tensor</span><span class="p">([</span><span class="kc">True</span><span class="p">,</span> <span class="kc">True</span><span class="p">,</span> <span class="kc">False</span><span class="p">])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">output</span> <span class="o">=</span> <span class="n">a</span><span class="o">.</span><span class="n">all</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">output</span><span class="p">)</span>
<span class="go">False</span>
</pre></div>
</div>
</dd></dl>

<dl class="method">
<dt id="mindspore.Tensor.any">
<code class="sig-name descname">any</code><span class="sig-paren">(</span><em class="sig-param">axis=()</em>, <em class="sig-param">keep_dims=False</em><span class="sig-paren">)</span><a class="headerlink" href="#mindspore.Tensor.any" title="Permalink to this definition">¶</a></dt>
<dd><p>检查在指定轴方向上是否存在任意为True的Tensor元素。</p>
<p><strong>参数：</strong></p>
<ul class="simple">
<li><p><strong>axis</strong> (Union[None, int, tuple(int)) - 计算any的维度。当 <cite>axis</cite> 为None或空元组时，计算所有维度。当 <cite>axis</cite> 为int或tuple(int)时，记Tensor的维度为dim，则其取值范围为[-dim, dim)。默认值：()。</p></li>
<li><p><strong>keep_dims</strong> (bool) - 计算结果是否保留维度。默认值：False。</p></li>
</ul>
<p><strong>返回：</strong></p>
<p>Tensor。如果在指定轴方向上所有Tensor元素都为True，则其值为True，否则其值为False。如果轴为None或空元组，则默认降维。</p>
<p><strong>支持平台：</strong></p>
<p><code class="docutils literal notranslate"><span class="pre">Ascend</span></code> <code class="docutils literal notranslate"><span class="pre">GPU</span></code> <code class="docutils literal notranslate"><span class="pre">CPU</span></code></p>
<p><strong>样例：</strong></p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">mindspore</span> <span class="kn">import</span> <span class="n">Tensor</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="n">Tensor</span><span class="p">([</span><span class="kc">True</span><span class="p">,</span> <span class="kc">True</span><span class="p">,</span> <span class="kc">False</span><span class="p">])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">output</span> <span class="o">=</span> <span class="n">a</span><span class="o">.</span><span class="n">any</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">output</span><span class="p">)</span>
<span class="go">True</span>
</pre></div>
</div>
</dd></dl>

<dl class="method">
<dt id="mindspore.Tensor.argmax">
<code class="sig-name descname">argmax</code><span class="sig-paren">(</span><em class="sig-param">axis=None</em><span class="sig-paren">)</span><a class="headerlink" href="#mindspore.Tensor.argmax" title="Permalink to this definition">¶</a></dt>
<dd><p>返回指定轴上最大值的索引。</p>
<p><strong>参数：</strong></p>
<ul class="simple">
<li><p><strong>axis</strong> (int, optional) - 默认情况下，返回扁平化Tensor的最大值序号，否则返回指定轴方向上。</p></li>
</ul>
<p><strong>返回：</strong></p>
<p>Tensor，最大值的索引。它与原始Tensor具有相同的shape，但移除了轴方向上的维度。</p>
<p><strong>异常：</strong></p>
<ul class="simple">
<li><p><strong>ValueError</strong> - 入参axis的设定值超出了范围。</p></li>
</ul>
<p><strong>支持平台：</strong></p>
<p><code class="docutils literal notranslate"><span class="pre">Ascend</span></code> <code class="docutils literal notranslate"><span class="pre">GPU</span></code> <code class="docutils literal notranslate"><span class="pre">CPU</span></code></p>
<p><strong>样例：</strong></p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">mindspore</span> <span class="kn">import</span> <span class="n">Tensor</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="n">Tensor</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">10</span><span class="p">,</span> <span class="mi">16</span><span class="p">)</span><span class="o">.</span><span class="n">reshape</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="s2">&quot;float32&quot;</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">a</span><span class="o">.</span><span class="n">argmax</span><span class="p">())</span>
<span class="go">5</span>
</pre></div>
</div>
</dd></dl>

<dl class="method">
<dt id="mindspore.Tensor.argmin">
<code class="sig-name descname">argmin</code><span class="sig-paren">(</span><em class="sig-param">axis=None</em><span class="sig-paren">)</span><a class="headerlink" href="#mindspore.Tensor.argmin" title="Permalink to this definition">¶</a></dt>
<dd><p>返回指定轴上最小值的索引。</p>
<p><strong>参数：</strong></p>
<ul class="simple">
<li><p><strong>axis</strong> (int, optional) - 返回扁平化Tensor的最小值序号，否则返回指定轴方向上的最小值序号。默认值: None。</p></li>
</ul>
<p><strong>返回：</strong></p>
<p>Tensor，最小Tensor的索引。它与原始Tensor具有相同的shape，但移除了轴方向上的维度。</p>
<p><strong>异常：</strong></p>
<ul class="simple">
<li><p><strong>ValueError</strong> - 入参axis的设定值超出了范围。</p></li>
</ul>
<p><strong>支持平台：</strong></p>
<p><code class="docutils literal notranslate"><span class="pre">Ascend</span></code> <code class="docutils literal notranslate"><span class="pre">GPU</span></code> <code class="docutils literal notranslate"><span class="pre">CPU</span></code></p>
<p><strong>样例：</strong></p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">mindspore</span> <span class="kn">import</span> <span class="n">Tensor</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="n">Tensor</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">10</span><span class="p">,</span> <span class="mi">16</span><span class="p">)</span><span class="o">.</span><span class="n">reshape</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="s2">&quot;float32&quot;</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">a</span><span class="o">.</span><span class="n">argmin</span><span class="p">())</span>
<span class="go">0</span>
</pre></div>
</div>
</dd></dl>

<dl class="method">
<dt id="mindspore.Tensor.asnumpy">
<code class="sig-name descname">asnumpy</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#mindspore.Tensor.asnumpy" title="Permalink to this definition">¶</a></dt>
<dd><p>将张量转换为NumPy数组。该方法会将Tensor本身转换为NumPy的ndarray。这个Tensor和函数返回的ndarray共享内存地址。对Tensor本身的修改会反映到相应的ndarray上。</p>
<p><strong>返回：</strong></p>
<p>NumPy的ndarray，该ndarray与Tensor共享内存地址。</p>
<p><strong>样例：</strong></p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">mindspore</span> <span class="kn">import</span> <span class="n">Tensor</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">=</span> <span class="n">Tensor</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">float32</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">y</span> <span class="o">=</span> <span class="n">x</span><span class="o">.</span><span class="n">asnumpy</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">y</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="mi">11</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="go">[11.  2.]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">y</span><span class="p">)</span>
<span class="go">[11.  2.]</span>
</pre></div>
</div>
</dd></dl>

<dl class="method">
<dt id="mindspore.Tensor.astype">
<code class="sig-name descname">astype</code><span class="sig-paren">(</span><em class="sig-param">dtype</em>, <em class="sig-param">copy=True</em><span class="sig-paren">)</span><a class="headerlink" href="#mindspore.Tensor.astype" title="Permalink to this definition">¶</a></dt>
<dd><p>将Tensor转为指定数据类型，可指定是否返回副本。</p>
<p><strong>参数：</strong></p>
<ul class="simple">
<li><p><strong>dtype</strong> (Union[<cite>mindspore.dtype</cite> , str]) - 指定的Tensor数据类型，可以是: <cite>mindspore.dtype.float32</cite> 或 <cite>float32</cite> 的格式。默认值：<cite>mindspore.dtype.float32</cite> 。</p></li>
<li><p><strong>copy</strong> (bool, optional) - 默认情况下，astype返回新拷贝的Tensor。如果该参数设为False，则返回输入Tensor而不是副本。默认值：True。</p></li>
</ul>
<p><strong>返回：</strong></p>
<p>Tensor，指定数据类型的Tensor。</p>
<p><strong>异常：</strong></p>
<ul class="simple">
<li><p><strong>TypeError</strong> - 指定了无法解析的类型。</p></li>
</ul>
<p><strong>支持平台：</strong></p>
<p><code class="docutils literal notranslate"><span class="pre">Ascend</span></code> <code class="docutils literal notranslate"><span class="pre">GPU</span></code> <code class="docutils literal notranslate"><span class="pre">CPU</span></code></p>
<p><strong>样例：</strong></p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">mindspore</span> <span class="kn">import</span> <span class="n">Tensor</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">=</span> <span class="n">Tensor</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">ones</span><span class="p">((</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">1</span><span class="p">),</span> <span class="n">dtype</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">float32</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">=</span> <span class="n">x</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="s2">&quot;int32&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">x</span><span class="o">.</span><span class="n">dtype</span><span class="p">)</span>
<span class="go">Int32</span>
</pre></div>
</div>
</dd></dl>

<dl class="method">
<dt id="mindspore.Tensor.choose">
<code class="sig-name descname">choose</code><span class="sig-paren">(</span><em class="sig-param">choices</em>, <em class="sig-param">mode='clip'</em><span class="sig-paren">)</span><a class="headerlink" href="#mindspore.Tensor.choose" title="Permalink to this definition">¶</a></dt>
<dd><p>根据原始Tensor数组和一个索引数组构造一个新的Tensor。</p>
<p><strong>参数：</strong></p>
<ul class="simple">
<li><p><strong>choices</strong> (Union[tuple, list, Tensor]) - 索引选择数组。原始输入Tensor和 <cite>choices</cite> 的广播维度必须相同。如果 <cite>choices</cite> 本身是一个Tensor，则其最外层的维度（即，对应于第0维的维度）被用来定义 <cite>choices</cite> 数组。</p></li>
<li><p><strong>mode</strong> (‘raise’, ‘wrap’, ‘clip’, optional) - 指定如何处理 <cite>[0, n-1]</cite> 外部的索引：</p>
<ul>
<li><p><strong>raise</strong> – 引发异常（默认）；</p></li>
<li><p><strong>wrap</strong> – 原值映射为对n取余后的值；</p></li>
<li><p><strong>clip</strong> – 大于n-1的值会被映射为n-1。该模式下禁用负数索引。</p></li>
</ul>
</li>
</ul>
<p><strong>返回：</strong></p>
<p>Tensor，合并后的结果。</p>
<p><strong>异常：</strong></p>
<ul class="simple">
<li><p><strong>ValueError</strong> - 输入Tensor和任一 <cite>choices</cite> 无法广播。</p></li>
</ul>
<p><strong>支持平台：</strong></p>
<p><code class="docutils literal notranslate"><span class="pre">Ascend</span></code> <code class="docutils literal notranslate"><span class="pre">GPU</span></code> <code class="docutils literal notranslate"><span class="pre">CPU</span></code></p>
<p><strong>样例：</strong></p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">mindspore</span> <span class="kn">import</span> <span class="n">Tensor</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">choices</span> <span class="o">=</span> <span class="p">[[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">],</span> <span class="p">[</span><span class="mi">10</span><span class="p">,</span> <span class="mi">11</span><span class="p">,</span> <span class="mi">12</span><span class="p">,</span> <span class="mi">13</span><span class="p">],</span> <span class="p">[</span><span class="mi">20</span><span class="p">,</span> <span class="mi">21</span><span class="p">,</span> <span class="mi">22</span><span class="p">,</span> <span class="mi">23</span><span class="p">],</span> <span class="p">[</span><span class="mi">30</span><span class="p">,</span> <span class="mi">31</span><span class="p">,</span> <span class="mi">32</span><span class="p">,</span> <span class="mi">33</span><span class="p">]]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">=</span> <span class="n">Tensor</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">]))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">x</span><span class="o">.</span><span class="n">choose</span><span class="p">(</span><span class="n">choices</span><span class="p">))</span>
<span class="go">[20 31 12  3]</span>
</pre></div>
</div>
</dd></dl>

<dl class="method">
<dt id="mindspore.Tensor.clip">
<code class="sig-name descname">clip</code><span class="sig-paren">(</span><em class="sig-param">xmin</em>, <em class="sig-param">xmax</em>, <em class="sig-param">dtype=None</em><span class="sig-paren">)</span><a class="headerlink" href="#mindspore.Tensor.clip" title="Permalink to this definition">¶</a></dt>
<dd><p>裁剪Tensor中的值。</p>
<p>给定一个区间，区间外的值将被裁剪到区间边缘。
例如，如果指定的间隔为 <span class="math notranslate nohighlight">\([0, 1]\)</span> ，则小于0的值将变为0，大于1的值将变为1。</p>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>目前不支持裁剪 <cite>xmin=nan</cite> 或 <cite>xmax=nan</cite> 。</p>
</div>
<p><strong>参数：</strong></p>
<ul class="simple">
<li><p><strong>xmin</strong> (Tensor, scalar, None) - 最小值。如果值为None，则不在间隔的下边缘执行裁剪操作。<cite>xmin</cite> 或 <cite>xmax</cite> 只能有一个为None。</p></li>
<li><p><strong>xmax</strong> (Tensor, scalar, None) - 最大值。如果值为None，则不在间隔的上边缘执行裁剪操作。<cite>xmin</cite> 或 <cite>xmax</cite> 只能有一个为None。如果 <cite>xmin</cite> 或 <cite>xmax</cite> 是Tensor，则三个Tensor将被广播进行shape匹配。</p></li>
<li><p><strong>dtype</strong> (<cite>mindspore.dtype</cite> , optional) - 覆盖输出Tensor的dtype。默认值为None。</p></li>
</ul>
<p><strong>返回：</strong></p>
<p>Tensor，含有输入Tensor的元素，其中values &lt; <cite>xmin</cite> 被替换为 <cite>xmin</cite> ，values &gt; <cite>xmax</cite> 被替换为 <cite>xmax</cite> 。</p>
<p><strong>异常：</strong></p>
<ul class="simple">
<li><p><strong>TypeError</strong> - 输入的类型与Tensor不一致。</p></li>
<li><p><strong>ValueError</strong> - 输入与Tensor的shape不能广播，或者 <cite>xmin</cite> 和 <cite>xmax</cite> 都是 <cite>None</cite> 。</p></li>
</ul>
<p><strong>支持平台：</strong></p>
<p><code class="docutils literal notranslate"><span class="pre">Ascend</span></code> <code class="docutils literal notranslate"><span class="pre">GPU</span></code> <code class="docutils literal notranslate"><span class="pre">CPU</span></code></p>
<p><strong>样例：</strong></p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">mindspore</span> <span class="kn">import</span> <span class="n">Tensor</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">=</span> <span class="n">Tensor</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="o">-</span><span class="mi">4</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">0</span><span class="p">])</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="s2">&quot;float32&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">y</span> <span class="o">=</span> <span class="n">x</span><span class="o">.</span><span class="n">clip</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">y</span><span class="p">)</span>
<span class="go">[1. 2. 2. 0. 0. 2. 2. 0.]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">t</span> <span class="o">=</span> <span class="n">Tensor</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">y</span> <span class="o">=</span> <span class="n">x</span><span class="o">.</span><span class="n">clip</span><span class="p">(</span><span class="n">t</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">y</span><span class="p">)</span>
<span class="go">[1. 2. 2. 1. 1. 2. 2. 1.]</span>
</pre></div>
</div>
</dd></dl>

<dl class="method">
<dt id="mindspore.Tensor.copy">
<code class="sig-name descname">copy</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#mindspore.Tensor.copy" title="Permalink to this definition">¶</a></dt>
<dd><p>复制一个Tensor并返回。</p>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>当前实现不支持类似NumPy的 <cite>order</cite> 参数。</p>
</div>
<p><strong>返回：</strong></p>
<p>复制的Tensor。</p>
<p><strong>支持平台：</strong></p>
<p><code class="docutils literal notranslate"><span class="pre">Ascend</span></code> <code class="docutils literal notranslate"><span class="pre">GPU</span></code> <code class="docutils literal notranslate"><span class="pre">CPU</span></code></p>
<p><strong>样例：</strong></p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">mindspore</span> <span class="kn">import</span> <span class="n">Tensor</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="n">Tensor</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">ones</span><span class="p">((</span><span class="mi">3</span><span class="p">,</span><span class="mi">3</span><span class="p">))</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="s2">&quot;float32&quot;</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">output</span> <span class="o">=</span> <span class="n">a</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">output</span><span class="p">)</span>
<span class="go">[[1. 1. 1.]</span>
<span class="go">[1. 1. 1.]</span>
<span class="go">[1. 1. 1.]]</span>
</pre></div>
</div>
</dd></dl>

<dl class="method">
<dt id="mindspore.Tensor.cumsum">
<code class="sig-name descname">cumsum</code><span class="sig-paren">(</span><em class="sig-param">axis=None</em>, <em class="sig-param">dtype=None</em><span class="sig-paren">)</span><a class="headerlink" href="#mindspore.Tensor.cumsum" title="Permalink to this definition">¶</a></dt>
<dd><p>返回指定轴方向上元素的累加值。</p>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>如果 <cite>dtype</cite> 为 <cite>int8</cite> , <cite>int16</cite> 或 <cite>bool</cite> ，则结果 <cite>dtype</cite> 将提升为 <cite>int32</cite> ，不支持 <cite>int64</cite> 。</p>
</div>
<p><strong>参数：</strong></p>
<ul class="simple">
<li><p><strong>axis</strong> (int, optional) - 轴，在该轴方向上的累积和。默认情况下，计算所有元素的累加和。</p></li>
<li><p><strong>dtype</strong> (<cite>mindspore.dtype</cite> , optional) - 如果未指定参数值，则保持与原始Tensor相同，除非参数值是一个精度小于 <cite>float32</cite> 的整数。在这种情况下，使用 <cite>float32</cite> 。默认值：None。</p></li>
</ul>
<p><strong>异常：</strong></p>
<ul class="simple">
<li><p><strong>ValueError</strong> - 轴超出范围。</p></li>
</ul>
<p><strong>返回：</strong></p>
<p>Tensor。</p>
<p><strong>支持平台：</strong></p>
<p><code class="docutils literal notranslate"><span class="pre">Ascend</span></code> <code class="docutils literal notranslate"><span class="pre">GPU</span></code> <code class="docutils literal notranslate"><span class="pre">CPU</span></code></p>
<p><strong>样例：</strong></p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">mindspore</span> <span class="kn">import</span> <span class="n">Tensor</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="n">Tensor</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">ones</span><span class="p">((</span><span class="mi">3</span><span class="p">,</span><span class="mi">3</span><span class="p">))</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="s2">&quot;float32&quot;</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">output</span> <span class="o">=</span> <span class="n">a</span><span class="o">.</span><span class="n">cumsum</span><span class="p">(</span><span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">output</span><span class="p">)</span>
<span class="go">[[1. 1. 1.]</span>
<span class="go">[2. 2. 2.]</span>
<span class="go">[3. 3. 3.]]</span>
</pre></div>
</div>
</dd></dl>

<dl class="method">
<dt id="mindspore.Tensor.diagonal">
<code class="sig-name descname">diagonal</code><span class="sig-paren">(</span><em class="sig-param">offset=0</em>, <em class="sig-param">axis1=0</em>, <em class="sig-param">axis2=1</em><span class="sig-paren">)</span><a class="headerlink" href="#mindspore.Tensor.diagonal" title="Permalink to this definition">¶</a></dt>
<dd><p>返回指定的对角线。</p>
<p><strong>参数：</strong></p>
<ul class="simple">
<li><p><strong>offset</strong> (int, optional) - 对角线与主对角线的偏移。可以是正值或负值。默认为主对角线。</p></li>
<li><p><strong>axis1</strong> (int, optional) - 二维子数组的第一轴，对角线应该从这里开始。默认为第一轴(0)。</p></li>
<li><p><strong>axis2</strong> (int, optional) - 二维子数组的第二轴，对角线应该从这里开始。默认为第二轴。</p></li>
</ul>
<p><strong>返回：</strong></p>
<p>Tensor，如果Tensor是二维，则返回值是一维数组。</p>
<p><strong>异常：</strong></p>
<ul class="simple">
<li><p><strong>ValueError</strong> - 输入Tensor的维度少于2。</p></li>
</ul>
<p><strong>支持平台：</strong></p>
<p><code class="docutils literal notranslate"><span class="pre">Ascend</span></code> <code class="docutils literal notranslate"><span class="pre">GPU</span></code> <code class="docutils literal notranslate"><span class="pre">CPU</span></code></p>
<p><strong>样例：</strong></p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">mindspore</span> <span class="kn">import</span> <span class="n">Tensor</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="n">Tensor</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">4</span><span class="p">)</span><span class="o">.</span><span class="n">reshape</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">2</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">a</span><span class="p">)</span>
<span class="go">[[0 1]</span>
<span class="go">[2 3]]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">output</span> <span class="o">=</span> <span class="n">a</span><span class="o">.</span><span class="n">diagonal</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">output</span><span class="p">)</span>
<span class="go">[0 3]</span>
</pre></div>
</div>
</dd></dl>

<dl class="method">
<dt id="mindspore.Tensor.dtype">
<em class="property">property </em><code class="sig-name descname">dtype</code><a class="headerlink" href="#mindspore.Tensor.dtype" title="Permalink to this definition">¶</a></dt>
<dd><p>返回张量的数据类型（<a class="reference internal" href="mindspore.dtype.html#mindspore.dtype" title="mindspore.dtype"><code class="xref py py-class docutils literal notranslate"><span class="pre">mindspore.dtype</span></code></a>）。</p>
</dd></dl>

<dl class="method">
<dt id="mindspore.Tensor.expand_as">
<code class="sig-name descname">expand_as</code><span class="sig-paren">(</span><em class="sig-param">x</em><span class="sig-paren">)</span><a class="headerlink" href="#mindspore.Tensor.expand_as" title="Permalink to this definition">¶</a></dt>
<dd><p>将目标张量的维度扩展为输入张量的维度。</p>
<p><strong>参数：</strong></p>
<ul class="simple">
<li><p><strong>x</strong> (Tensor) - 输入的张量。</p></li>
</ul>
<p><strong>返回：</strong></p>
<p>维度与输入张量的相同的Tensor。输出张量的维度必须遵守广播规则。广播规则指输出张量的维度需要扩展为输入张量的维度，如果目标张量的维度大于输入张量的维度，则不满足广播规则。</p>
<p><strong>样例：</strong></p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">mindspore</span> <span class="kn">import</span> <span class="n">Tensor</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">mindspore</span> <span class="kn">import</span> <span class="n">dtype</span> <span class="k">as</span> <span class="n">mstype</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">=</span> <span class="n">Tensor</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="n">mstype</span><span class="o">.</span><span class="n">float32</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">y</span> <span class="o">=</span> <span class="n">Tensor</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">ones</span><span class="p">((</span><span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">)),</span> <span class="n">dtype</span><span class="o">=</span><span class="n">mstype</span><span class="o">.</span><span class="n">float32</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">output</span> <span class="o">=</span> <span class="n">x</span><span class="o">.</span><span class="n">expand_as</span><span class="p">(</span><span class="n">y</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">output</span><span class="p">)</span>
<span class="go">[[1. 2. 3.]</span>
<span class="go">[1. 2. 3.]]</span>
</pre></div>
</div>
</dd></dl>

<dl class="method">
<dt id="mindspore.Tensor.fill">
<code class="sig-name descname">fill</code><span class="sig-paren">(</span><em class="sig-param">value</em><span class="sig-paren">)</span><a class="headerlink" href="#mindspore.Tensor.fill" title="Permalink to this definition">¶</a></dt>
<dd><p>用标量值填充数组。</p>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>与NumPy不同，Tensor.fill()将始终返回一个新的Tensor，而不是填充原来的Tensor。</p>
</div>
<p><strong>参数：</strong></p>
<ul class="simple">
<li><p><strong>value</strong> (Union[None, int, float, bool]) - 所有元素都被赋予这个值。</p></li>
</ul>
<p><strong>返回：</strong></p>
<p>Tensor，与原来的dtype和shape相同的Tensor。</p>
<p><strong>异常：</strong></p>
<ul class="simple">
<li><p><strong>TypeError</strong> - 输入参数具有前面未指定的类型。</p></li>
</ul>
<p><strong>支持平台：</strong></p>
<p><code class="docutils literal notranslate"><span class="pre">Ascend</span></code> <code class="docutils literal notranslate"><span class="pre">GPU</span></code> <code class="docutils literal notranslate"><span class="pre">CPU</span></code></p>
<p><strong>样例：</strong></p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">mindspore</span> <span class="kn">import</span> <span class="n">Tensor</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="n">Tensor</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">4</span><span class="p">)</span><span class="o">.</span><span class="n">reshape</span><span class="p">((</span><span class="mi">2</span><span class="p">,</span><span class="mi">2</span><span class="p">))</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="s1">&#39;float32&#39;</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">a</span><span class="o">.</span><span class="n">fill</span><span class="p">(</span><span class="mf">1.0</span><span class="p">))</span>
<span class="go">[[1. 1.]</span>
<span class="go">[1. 1.]]</span>
</pre></div>
</div>
</dd></dl>

<dl class="method">
<dt id="mindspore.Tensor.flatten">
<code class="sig-name descname">flatten</code><span class="sig-paren">(</span><em class="sig-param">order='C'</em><span class="sig-paren">)</span><a class="headerlink" href="#mindspore.Tensor.flatten" title="Permalink to this definition">¶</a></dt>
<dd><p>返回展开成一维的Tensor的副本。</p>
<p><strong>参数：</strong></p>
<p><strong>order</strong> (str, optional) - 可以在’C’和’F’之间进行选择。’C’表示按行优先（C风格）顺序展开。’F’表示按列优先顺序（Fortran风格）进行扁平化。仅支持’C’和’F’。默认值：’C’。</p>
<p><strong>返回：</strong></p>
<p>Tensor，具有与输入相同的数据类型。</p>
<p><strong>异常：</strong></p>
<ul class="simple">
<li><p><strong>TypeError</strong> - <cite>order</cite> 不是字符串类型。</p></li>
<li><p><strong>ValueError</strong> - <cite>order</cite> 是字符串类型，但不是’C’或’F’。</p></li>
</ul>
<p><strong>支持平台：</strong></p>
<p><code class="docutils literal notranslate"><span class="pre">Ascend</span></code> <code class="docutils literal notranslate"><span class="pre">GPU</span></code> <code class="docutils literal notranslate"><span class="pre">CPU</span></code></p>
<p><strong>样例：</strong></p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">mindspore</span> <span class="kn">import</span> <span class="n">Tensor</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">=</span> <span class="n">Tensor</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">ones</span><span class="p">((</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">4</span><span class="p">),</span> <span class="n">dtype</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">float32</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">output</span> <span class="o">=</span> <span class="n">x</span><span class="o">.</span><span class="n">flatten</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">output</span><span class="o">.</span><span class="n">shape</span><span class="p">)</span>
<span class="go">(24,)</span>
</pre></div>
</div>
</dd></dl>

<dl class="method">
<dt id="mindspore.Tensor.flush_from_cache">
<code class="sig-name descname">flush_from_cache</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#mindspore.Tensor.flush_from_cache" title="Permalink to this definition">¶</a></dt>
<dd><p>如果Tensor开启缓存作用，则将缓存数据刷新到host侧。</p>
<p><strong>样例：</strong></p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">mindspore</span> <span class="kn">import</span> <span class="n">Tensor</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">=</span> <span class="n">Tensor</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">float32</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">y</span> <span class="o">=</span> <span class="n">x</span><span class="o">.</span><span class="n">flush_from_cache</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">y</span><span class="p">)</span>
<span class="go">None</span>
</pre></div>
</div>
</dd></dl>

<dl class="method">
<dt id="mindspore.Tensor.from_numpy">
<em class="property">static </em><code class="sig-name descname">from_numpy</code><span class="sig-paren">(</span><em class="sig-param">array</em><span class="sig-paren">)</span><a class="headerlink" href="#mindspore.Tensor.from_numpy" title="Permalink to this definition">¶</a></dt>
<dd><p>通过不复制数据的方式将Numpy数组转换为张量。</p>
<p><strong>参数：</strong></p>
<p><strong>array</strong> (numpy.array) - 输入数组。</p>
<p><strong>返回：</strong></p>
<p>与输入的张量具有相同的数据类型的Tensor。</p>
<p><strong>样例：</strong></p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">mindspore</span> <span class="kn">import</span> <span class="n">Tensor</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">output</span> <span class="o">=</span> <span class="n">Tensor</span><span class="o">.</span><span class="n">from_numpy</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">output</span><span class="p">)</span>
<span class="go">[1 2]</span>
</pre></div>
</div>
</dd></dl>

<dl class="method">
<dt id="mindspore.Tensor.has_init">
<em class="property">property </em><code class="sig-name descname">has_init</code><a class="headerlink" href="#mindspore.Tensor.has_init" title="Permalink to this definition">¶</a></dt>
<dd><p>Tensor是否已经初始化。</p>
</dd></dl>

<dl class="method">
<dt id="mindspore.Tensor.init_data">
<code class="sig-name descname">init_data</code><span class="sig-paren">(</span><em class="sig-param">slice_index=None</em>, <em class="sig-param">shape=None</em>, <em class="sig-param">opt_shard_group=None</em><span class="sig-paren">)</span><a class="headerlink" href="#mindspore.Tensor.init_data" title="Permalink to this definition">¶</a></dt>
<dd><p>获取此Tensor的数据。</p>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>对于同一个Tensor，只可以调用一次 <cite>init_data</cite> 函数。</p>
</div>
<p><strong>参数：</strong></p>
<ul class="simple">
<li><p><strong>slice_index</strong> (int) - 参数切片的索引。在初始化参数切片的时候使用，保证使用相同切片的设备可以生成相同的Tensor。默认值：None。</p></li>
<li><p><strong>shape</strong> (list[int]) - 切片的shape，在初始化参数切片时使用。默认值：None。</p></li>
<li><p><strong>opt_shard_group</strong> (str) - 优化器分片组，在自动或半自动并行模式下用于获取参数的切片。默认值：None。</p></li>
</ul>
<p><strong>返回：</strong></p>
<p>初始化的Tensor。</p>
<p><strong>支持平台：</strong></p>
<p><code class="docutils literal notranslate"><span class="pre">Ascend</span></code> <code class="docutils literal notranslate"><span class="pre">GPU</span></code> <code class="docutils literal notranslate"><span class="pre">CPU</span></code></p>
<p><strong>样例：</strong></p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">mindspore</span> <span class="k">as</span> <span class="nn">ms</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">mindspore.common.initializer</span> <span class="k">as</span> <span class="nn">init</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">=</span> <span class="n">init</span><span class="o">.</span><span class="n">initializer</span><span class="p">(</span><span class="n">init</span><span class="o">.</span><span class="n">Constant</span><span class="p">(</span><span class="mi">1</span><span class="p">),</span> <span class="p">[</span><span class="mi">2</span><span class="p">,</span> <span class="mi">2</span><span class="p">],</span> <span class="n">ms</span><span class="o">.</span><span class="n">float32</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">out</span> <span class="o">=</span> <span class="n">x</span><span class="o">.</span><span class="n">init_data</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">out</span><span class="p">)</span>
<span class="go">[[1. 1.]</span>
<span class="go">[1. 1.]]</span>
</pre></div>
</div>
</dd></dl>

<dl class="method">
<dt id="mindspore.Tensor.item">
<code class="sig-name descname">item</code><span class="sig-paren">(</span><em class="sig-param">index=None</em><span class="sig-paren">)</span><a class="headerlink" href="#mindspore.Tensor.item" title="Permalink to this definition">¶</a></dt>
<dd><p>获取Tensor中指定索引的元素。</p>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>Tensor.item返回的是Tensor标量，而不是Python标量。</p>
</div>
<p><strong>参数：</strong></p>
<ul class="simple">
<li><p><strong>index</strong> (Union[None, int, tuple(int)]) - Tensor的索引。默认值：None。</p></li>
</ul>
<p><strong>返回：</strong></p>
<p>Tensor标量，dtype与原始Tensor的相同。</p>
<p><strong>异常：</strong></p>
<ul class="simple">
<li><p><strong>ValueError</strong> - <cite>index</cite> 的长度不等于Tensor的ndim。</p></li>
</ul>
<p><strong>支持平台：</strong></p>
<p><code class="docutils literal notranslate"><span class="pre">Ascend</span></code> <code class="docutils literal notranslate"><span class="pre">GPU</span></code> <code class="docutils literal notranslate"><span class="pre">CPU</span></code></p>
<p><strong>样例：</strong></p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">mindspore</span> <span class="kn">import</span> <span class="n">Tensor</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">=</span> <span class="n">Tensor</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([[</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">],[</span><span class="mi">4</span><span class="p">,</span><span class="mi">5</span><span class="p">,</span><span class="mi">6</span><span class="p">]],</span> <span class="n">dtype</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">float32</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">=</span> <span class="n">x</span><span class="o">.</span><span class="n">item</span><span class="p">((</span><span class="mi">0</span><span class="p">,</span><span class="mi">1</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="go">2.0</span>
</pre></div>
</div>
</dd></dl>

<dl class="method">
<dt id="mindspore.Tensor.itemset">
<code class="sig-name descname">itemset</code><span class="sig-paren">(</span><em class="sig-param">*args</em><span class="sig-paren">)</span><a class="headerlink" href="#mindspore.Tensor.itemset" title="Permalink to this definition">¶</a></dt>
<dd><p>将标量插入到Tensor（并将标量转换为Tensor的数据类型）。</p>
<p>至少有1个参数，并且最后一个参数被定义为设定值。
Tensor.itemset(*args)等同于 <span class="math notranslate nohighlight">\(Tensor[args] = item\)</span> 。</p>
<p><strong>参数：</strong></p>
<p><strong>args</strong> (Union[(numbers.Number), (int/tuple(int), numbers.Number)]) - 指定索引和值的参数。如果 <cite>args</cite> 包含一个参数（标量），则其仅在Tensor大小为1的情况下使用。如果 <cite>args</cite> 包含两个参数，则最后一个参数是要设置的值且必须是标量，而第一个参数指定单个Tensor元素的位置。参数值是整数或者元组。</p>
<p><strong>返回：</strong></p>
<p>一个新的Tensor，其值为 <span class="math notranslate nohighlight">\(Tensor[args] = item\)</span> 。</p>
<p><strong>异常：</strong></p>
<ul class="simple">
<li><p><strong>ValueError</strong> - 第一个参数的长度不等于Tensor的ndim。</p></li>
<li><p><strong>IndexError</strong> - 只提供了一个参数，并且原来的Tensor不是标量。</p></li>
</ul>
<p><strong>支持平台：</strong></p>
<p><code class="docutils literal notranslate"><span class="pre">Ascend</span></code> <code class="docutils literal notranslate"><span class="pre">GPU</span></code> <code class="docutils literal notranslate"><span class="pre">CPU</span></code></p>
<p><strong>样例：</strong></p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">mindspore</span> <span class="kn">import</span> <span class="n">Tensor</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">=</span> <span class="n">Tensor</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([[</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">],[</span><span class="mi">4</span><span class="p">,</span><span class="mi">5</span><span class="p">,</span><span class="mi">6</span><span class="p">]],</span> <span class="n">dtype</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">float32</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">x</span><span class="o">.</span><span class="n">itemset</span><span class="p">((</span><span class="mi">0</span><span class="p">,</span><span class="mi">1</span><span class="p">),</span> <span class="mi">4</span><span class="p">))</span>
<span class="go">[[1. 4. 3.]</span>
<span class="go">[4. 5. 6.]]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="go">[[1. 2. 3.]</span>
<span class="go">[4. 5. 6.]]</span>
</pre></div>
</div>
</dd></dl>

<dl class="method">
<dt id="mindspore.Tensor.itemsize">
<em class="property">property </em><code class="sig-name descname">itemsize</code><a class="headerlink" href="#mindspore.Tensor.itemsize" title="Permalink to this definition">¶</a></dt>
<dd><p>返回一个Tensor元素的长度（以字节为单位）。</p>
</dd></dl>

<dl class="method">
<dt id="mindspore.Tensor.max">
<code class="sig-name descname">max</code><span class="sig-paren">(</span><em class="sig-param">axis=None</em>, <em class="sig-param">keepdims=False</em>, <em class="sig-param">initial=None</em>, <em class="sig-param">where=True</em><span class="sig-paren">)</span><a class="headerlink" href="#mindspore.Tensor.max" title="Permalink to this definition">¶</a></dt>
<dd><p>返回Tensor的最大值或轴方向上的最大值。</p>
<p><strong>参数：</strong></p>
<ul class="simple">
<li><p><strong>axis</strong> (Union[None, int, tuple of ints], optional) - 轴，在该轴方向上进行操作。默认情况下，使用扁平输入。如果该参数为整数元组，则在多个轴上选择最大值，而不是在单个轴或所有轴上进行选择。默认值：None。</p></li>
<li><p><strong>keepdims</strong> (bool, optional) - 如果这个参数为True，被删去的维度保留在结果中，且维度大小设为1。有了这个选项，结果就可以与输入数组进行正确的广播运算。默认值：False。</p></li>
<li><p><strong>initial</strong> (scalar, optional) - 输出元素的最小值。如果对空切片进行计算，则该参数必须设置。默认值：None。</p></li>
<li><p><strong>where</strong> (bool Tensor, optional) - 一个bool数组，被广播以匹配数组维度和选择包含在降维中的元素。如果传递了一个非默认值，则还必须提供初始值。默认值：True。</p></li>
</ul>
<p><strong>返回：</strong></p>
<p>Tensor或标量，输入Tensor的最大值。如果 <cite>axis</cite> 为None，则结果是一个标量值。如果提供了 <cite>axis</cite> ，则结果是Tensor ndim - 1维度的一个数组。</p>
<p><strong>异常：</strong></p>
<ul class="simple">
<li><p><strong>TypeError</strong> - 参数具有前面未指定的类型。</p></li>
</ul>
<p><strong>支持平台：</strong></p>
<p><code class="docutils literal notranslate"><span class="pre">Ascend</span></code> <code class="docutils literal notranslate"><span class="pre">GPU</span></code> <code class="docutils literal notranslate"><span class="pre">CPU</span></code></p>
<p><strong>样例：</strong></p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">mindspore</span> <span class="kn">import</span> <span class="n">Tensor</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="n">Tensor</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">4</span><span class="p">)</span><span class="o">.</span><span class="n">reshape</span><span class="p">((</span><span class="mi">2</span><span class="p">,</span> <span class="mi">2</span><span class="p">))</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="s1">&#39;float32&#39;</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">output</span> <span class="o">=</span> <span class="n">a</span><span class="o">.</span><span class="n">max</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">output</span><span class="p">)</span>
<span class="go">3.0</span>
</pre></div>
</div>
</dd></dl>

<dl class="method">
<dt id="mindspore.Tensor.mean">
<code class="sig-name descname">mean</code><span class="sig-paren">(</span><em class="sig-param">axis=()</em>, <em class="sig-param">keep_dims=False</em><span class="sig-paren">)</span><a class="headerlink" href="#mindspore.Tensor.mean" title="Permalink to this definition">¶</a></dt>
<dd><p>返回指定维度上所有元素的均值，并降维。</p>
<p><strong>参数：</strong></p>
<ul class="simple">
<li><p><strong>axis</strong> (Union[None, int, tuple(int), list(int)]) - 计算mean的维度。当 <cite>axis</cite> 为None或空元组时，计算所有维度。当 <cite>axis</cite> 为int、tuple(int)或list(int)时，记Tensor的维度为dim，则其取值范围为[-dim, dim)。默认值：()。</p></li>
<li><p><strong>keep_dims</strong> (bool) - 计算结果是否保留维度。默认值：False。</p></li>
</ul>
<p><strong>返回：</strong></p>
<p>与输入的张量具有相同的数据类型的Tensor。</p>
<p><strong>支持平台：</strong></p>
<p><code class="docutils literal notranslate"><span class="pre">Ascend</span></code> <code class="docutils literal notranslate"><span class="pre">GPU</span></code> <code class="docutils literal notranslate"><span class="pre">CPU</span></code></p>
<p><strong>样例：</strong></p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">mindspore</span> <span class="kn">import</span> <span class="n">Tensor</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">input_x</span> <span class="o">=</span> <span class="n">Tensor</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">float32</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">output</span> <span class="o">=</span> <span class="n">input_x</span><span class="o">.</span><span class="n">mean</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">output</span><span class="p">)</span>
<span class="go">2.0</span>
</pre></div>
</div>
</dd></dl>

<dl class="method">
<dt id="mindspore.Tensor.min">
<code class="sig-name descname">min</code><span class="sig-paren">(</span><em class="sig-param">axis=None</em>, <em class="sig-param">keepdims=False</em>, <em class="sig-param">initial=None</em>, <em class="sig-param">where=True</em><span class="sig-paren">)</span><a class="headerlink" href="#mindspore.Tensor.min" title="Permalink to this definition">¶</a></dt>
<dd><p>返回Tensor的最小值或轴方向上的最小值。</p>
<p><strong>参数：</strong></p>
<ul class="simple">
<li><p><strong>axis</strong> (Union[None, int, tuple of ints], optional) - 轴，在该轴方向上进行操作。默认情况下，使用扁平输入。如果该参数为整数元组，则在多个轴上选择最小值，而不是在单个轴或所有轴上进行选择。默认值：None。</p></li>
<li><p><strong>keepdims</strong> (bool, optional) - 如果这个参数为True，被删去的维度保留在结果中，且维度大小设为1。有了这个选项，结果就可以与输入数组进行正确的广播运算。默认值：False。</p></li>
<li><p><strong>initial</strong> (scalar, optional) - 输出元素的最大值。如果对空切片进行计算，则该参数必须设置。默认值：None。</p></li>
<li><p><strong>where</strong> (bool Tensor, optional) - 一个布尔数组，被广播以匹配数组维度和选择包含在降维中的元素。如果传递了一个非默认值，则还必须提供初始值。默认值：True。</p></li>
</ul>
<p><strong>返回：</strong></p>
<p>Tensor或标量，输入Tensor的最小值。如果轴为None，则结果为一个标量值。如果提供了 <cite>axis</cite> ，则结果是Tensor.ndim - 1维度的一个数组。</p>
<p><strong>异常：</strong></p>
<ul class="simple">
<li><p><strong>TypeError</strong> - 参数具有前面未指定的类型。</p></li>
</ul>
<p><strong>支持平台：</strong></p>
<p><code class="docutils literal notranslate"><span class="pre">Ascend</span></code> <code class="docutils literal notranslate"><span class="pre">GPU</span></code> <code class="docutils literal notranslate"><span class="pre">CPU</span></code></p>
<p><strong>样例：</strong></p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">mindspore</span> <span class="kn">import</span> <span class="n">Tensor</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">mindspore.numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="n">Tensor</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">4</span><span class="p">)</span><span class="o">.</span><span class="n">reshape</span><span class="p">((</span><span class="mi">2</span><span class="p">,</span><span class="mi">2</span><span class="p">))</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="s1">&#39;float32&#39;</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">output</span> <span class="o">=</span> <span class="n">a</span><span class="o">.</span><span class="n">min</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">output</span><span class="p">)</span>
<span class="go">0.0</span>
</pre></div>
</div>
</dd></dl>

<dl class="method">
<dt id="mindspore.Tensor.nbytes">
<em class="property">property </em><code class="sig-name descname">nbytes</code><a class="headerlink" href="#mindspore.Tensor.nbytes" title="Permalink to this definition">¶</a></dt>
<dd><p>返回Tensor占用的总字节数。</p>
</dd></dl>

<dl class="method">
<dt id="mindspore.Tensor.ndim">
<em class="property">property </em><code class="sig-name descname">ndim</code><a class="headerlink" href="#mindspore.Tensor.ndim" title="Permalink to this definition">¶</a></dt>
<dd><p>返回Tensor维度的数量。</p>
</dd></dl>

<dl class="method">
<dt id="mindspore.Tensor.ptp">
<code class="sig-name descname">ptp</code><span class="sig-paren">(</span><em class="sig-param">axis=None</em>, <em class="sig-param">keepdims=False</em><span class="sig-paren">)</span><a class="headerlink" href="#mindspore.Tensor.ptp" title="Permalink to this definition">¶</a></dt>
<dd><p>该函数名称是”peak to peak”的缩写。计算沿着axis的最大值与最小值的差值。</p>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>不支持NumPy参数 <cite>dtype</cite> 和 <cite>out</cite> 。</p>
</div>
<p><strong>参数：</strong></p>
<ul class="simple">
<li><p><strong>axis</strong> (Union[None, int, tuple(int)]) - 轴，在轴方向上可以计算范围。默认计算扁平数组的方差。默认值：None。</p></li>
<li><p><strong>keepdims</strong> (bool) - 如果设为True，被删去的维度保留在结果中，且维度大小设为1。有了这个选项，结果将针对输入数组正确传递。默认值为False。</p></li>
</ul>
<p><strong>返回：</strong></p>
<p>Tensor。</p>
<p><strong>异常：</strong></p>
<ul class="simple">
<li><p><strong>TypeError</strong> - <cite>self</cite> 不是Tensor，或者 <cite>axis</cite> 和 <cite>keepdims</cite> 具有前面未指定的类型。</p></li>
</ul>
<p><strong>支持平台：</strong></p>
<p><code class="docutils literal notranslate"><span class="pre">Ascend</span></code> <code class="docutils literal notranslate"><span class="pre">GPU</span></code> <code class="docutils literal notranslate"><span class="pre">CPU</span></code></p>
<p><strong>样例：</strong></p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">mindspore</span> <span class="kn">import</span> <span class="n">Tensor</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">=</span> <span class="n">Tensor</span><span class="p">([[</span><span class="mf">4.0</span><span class="p">,</span> <span class="mf">9.0</span><span class="p">,</span> <span class="mf">2.0</span><span class="p">,</span> <span class="mf">10.0</span><span class="p">],</span> <span class="p">[</span><span class="mf">6.0</span><span class="p">,</span> <span class="mf">9.0</span><span class="p">,</span> <span class="mf">7.0</span><span class="p">,</span> <span class="mf">12.0</span><span class="p">]])</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="s2">&quot;float32&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">x</span><span class="o">.</span><span class="n">ptp</span><span class="p">(</span><span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">))</span>
<span class="go">[8. 6.]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">x</span><span class="o">.</span><span class="n">ptp</span><span class="p">(</span><span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">))</span>
<span class="go">[2. 0. 5. 2.]</span>
</pre></div>
</div>
</dd></dl>

<dl class="method">
<dt id="mindspore.Tensor.ravel">
<code class="sig-name descname">ravel</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#mindspore.Tensor.ravel" title="Permalink to this definition">¶</a></dt>
<dd><p>返回一个展开的一维Tensor。</p>
<p><strong>返回：</strong></p>
<p>一维Tensor，含有与输入相同的元素。</p>
<p><strong>支持平台：</strong></p>
<p><code class="docutils literal notranslate"><span class="pre">Ascend</span></code> <code class="docutils literal notranslate"><span class="pre">GPU</span></code> <code class="docutils literal notranslate"><span class="pre">CPU</span></code></p>
<p><strong>样例：</strong></p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">mindspore</span> <span class="kn">import</span> <span class="n">Tensor</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">=</span> <span class="n">Tensor</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">ones</span><span class="p">((</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">4</span><span class="p">),</span> <span class="n">dtype</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">float32</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">output</span> <span class="o">=</span> <span class="n">x</span><span class="o">.</span><span class="n">ravel</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">output</span><span class="o">.</span><span class="n">shape</span><span class="p">)</span>
<span class="go">(24,)</span>
</pre></div>
</div>
</dd></dl>

<dl class="method">
<dt id="mindspore.Tensor.repeat">
<code class="sig-name descname">repeat</code><span class="sig-paren">(</span><em class="sig-param">repeats</em>, <em class="sig-param">axis=None</em><span class="sig-paren">)</span><a class="headerlink" href="#mindspore.Tensor.repeat" title="Permalink to this definition">¶</a></dt>
<dd><p>对数组中的元素进行重复复制。</p>
<p><strong>参数：</strong></p>
<ul class="simple">
<li><p><strong>repeats</strong> (Union[int, tuple, list]) - 每个元素的重复次数，<cite>repeats</cite> 被广播以适应指定轴的shape。</p></li>
<li><p><strong>axis</strong> (int, optional) - 轴方向上的重复值。默认情况下，使用展开的输入Tensor，并返回一个展开的输出Tensor。</p></li>
</ul>
<p><strong>返回：</strong></p>
<p>Tensor，除了维度外，与输入Tensor具有相同的shape。</p>
<p><strong>异常：</strong></p>
<ul class="simple">
<li><p><strong>ValueError</strong> - 维度超出范围。</p></li>
<li><p><strong>TypeError</strong> - 参数类型不匹配。</p></li>
</ul>
<p><strong>支持平台：</strong></p>
<p><code class="docutils literal notranslate"><span class="pre">Ascend</span></code> <code class="docutils literal notranslate"><span class="pre">GPU</span></code> <code class="docutils literal notranslate"><span class="pre">CPU</span></code></p>
<p><strong>样例：</strong></p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">mindspore</span> <span class="kn">import</span> <span class="n">Tensor</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">=</span> <span class="n">Tensor</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="mi">3</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">x</span><span class="o">.</span><span class="n">repeat</span><span class="p">(</span><span class="mi">4</span><span class="p">))</span>
<span class="go">[3 3 3 3]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">=</span> <span class="n">Tensor</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">],[</span><span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">]]))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">x</span><span class="o">.</span><span class="n">repeat</span><span class="p">(</span><span class="mi">2</span><span class="p">))</span>
<span class="go">[1 1 2 2 3 3 4 4]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">x</span><span class="o">.</span><span class="n">repeat</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">))</span>
<span class="go">[[1 1 1 2 2 2]</span>
<span class="go">[3 3 3 4 4 4]]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">x</span><span class="o">.</span><span class="n">repeat</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">],</span> <span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">))</span>
<span class="go">[[1 2]</span>
<span class="go">[3 4]</span>
<span class="go">[3 4]]</span>
</pre></div>
</div>
</dd></dl>

<dl class="method">
<dt id="mindspore.Tensor.reshape">
<code class="sig-name descname">reshape</code><span class="sig-paren">(</span><em class="sig-param">*shape</em><span class="sig-paren">)</span><a class="headerlink" href="#mindspore.Tensor.reshape" title="Permalink to this definition">¶</a></dt>
<dd><p>不改变数据的情况下，将Tensor的shape改为输入的新shape。</p>
<p><strong>参数：</strong></p>
<p><strong>shape</strong> (Union[int, tuple(int), list(int)]) - 新的shape应与原来的shape兼容。如果参数值为整数，则结果是该长度的一维数组。shape的维度可以为-1。在这种情况下，将根据数组的长度和剩下的维度计算出该值。</p>
<p><strong>返回：</strong></p>
<p>Tensor，具有新shape的Tensor。</p>
<p><strong>异常：</strong></p>
<ul class="simple">
<li><p><strong>TypeError</strong> - 新shape不是整数、列表或元组。</p></li>
<li><p><strong>ValueError</strong> - 新shape与原来Tensor的shape不兼容。</p></li>
</ul>
<p><strong>支持平台：</strong></p>
<p><code class="docutils literal notranslate"><span class="pre">Ascend</span></code> <code class="docutils literal notranslate"><span class="pre">GPU</span></code> <code class="docutils literal notranslate"><span class="pre">CPU</span></code></p>
<p><strong>样例：</strong></p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">mindspore</span> <span class="kn">import</span> <span class="n">Tensor</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">mindspore</span> <span class="kn">import</span> <span class="n">dtype</span> <span class="k">as</span> <span class="n">mstype</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">=</span> <span class="n">Tensor</span><span class="p">([[</span><span class="o">-</span><span class="mf">0.1</span><span class="p">,</span> <span class="mf">0.3</span><span class="p">,</span> <span class="mf">3.6</span><span class="p">],</span> <span class="p">[</span><span class="mf">0.4</span><span class="p">,</span> <span class="mf">0.5</span><span class="p">,</span> <span class="o">-</span><span class="mf">3.2</span><span class="p">]],</span> <span class="n">dtype</span><span class="o">=</span><span class="n">mstype</span><span class="o">.</span><span class="n">float32</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">output</span> <span class="o">=</span> <span class="n">x</span><span class="o">.</span><span class="n">reshape</span><span class="p">((</span><span class="mi">3</span><span class="p">,</span> <span class="mi">2</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">output</span><span class="p">)</span>
<span class="go">[[-0.1  0.3]</span>
<span class="go">[ 3.6  0.4]</span>
<span class="go">[ 0.5 -3.2]]</span>
</pre></div>
</div>
</dd></dl>

<dl class="method">
<dt id="mindspore.Tensor.resize">
<code class="sig-name descname">resize</code><span class="sig-paren">(</span><em class="sig-param">*new_shape</em><span class="sig-paren">)</span><a class="headerlink" href="#mindspore.Tensor.resize" title="Permalink to this definition">¶</a></dt>
<dd><p>将Tensor改为输入的新shape, 并将不足的元素补0。</p>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>此方法不更改输入数组的大小，也不返回NumPy中的任何内容，而是返回一个具有输入大小的新Tensor。不支持Numpy参数 <cite>refcheck</cite> 。</p>
</div>
<p><strong>参数：</strong></p>
<p><strong>new_shape</strong> (Union[ints, tuple of ints]) - 指定Tensor的新shape。</p>
<p><strong>返回：</strong></p>
<p>Tensor。</p>
<p><strong>支持平台：</strong></p>
<p><code class="docutils literal notranslate"><span class="pre">Ascend</span></code> <code class="docutils literal notranslate"><span class="pre">GPU</span></code> <code class="docutils literal notranslate"><span class="pre">CPU</span></code></p>
<p><strong>样例：</strong></p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">mindspore</span> <span class="kn">import</span> <span class="n">Tensor</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">=</span> <span class="n">Tensor</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">],</span> <span class="p">[</span><span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">6</span><span class="p">]],</span> <span class="n">dtype</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">float32</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">y</span> <span class="o">=</span> <span class="n">x</span><span class="o">.</span><span class="n">resize</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">y</span><span class="p">)</span>
<span class="go">[[1. 2. 3.]</span>
<span class="go">[4. 5. 6.]</span>
<span class="go">[0. 0. 0.]]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">y</span> <span class="o">=</span> <span class="n">x</span><span class="o">.</span><span class="n">resize</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">y</span><span class="p">)</span>
<span class="go">[[1. 2.]</span>
<span class="go">[3. 4.]]</span>
</pre></div>
</div>
</dd></dl>

<dl class="method">
<dt id="mindspore.Tensor.searchsorted">
<code class="sig-name descname">searchsorted</code><span class="sig-paren">(</span><em class="sig-param">v</em>, <em class="sig-param">side='left'</em>, <em class="sig-param">sorter=None</em><span class="sig-paren">)</span><a class="headerlink" href="#mindspore.Tensor.searchsorted" title="Permalink to this definition">¶</a></dt>
<dd><p>查找应插入元素以保存顺序的位置索引。</p>
<p><strong>参数：</strong></p>
<ul class="simple">
<li><p><strong>v</strong> (Union[int, float, bool, list, tuple, Tensor]) - 要插入元素的值。</p></li>
<li><p><strong>side</strong> (‘left’, ‘right’, optional) - 如果参数值为’left’，则给出找到的第一个合适位置的索引。如果参数值为’right’，则返回最后一个这样的索引。如果没有合适的索引，则返回0或N（其中N是Tensor的长度）。默认值：’left’。</p></li>
<li><p><strong>sorter</strong> (Union[int, float, bool, list, tuple, Tensor]) - 整数索引的可选一维数组，将Tensor按升序排序。它们通常是NumPy argsort方法的结果。</p></li>
</ul>
<p><strong>返回：</strong></p>
<p>Tensor，shape与 <cite>v</cite> 相同的插入点数组。</p>
<p><strong>异常：</strong></p>
<ul class="simple">
<li><p><strong>ValueError</strong> - <cite>side</cite> 或 <cite>sorter</cite> 的参数无效。</p></li>
</ul>
<p><strong>支持平台：</strong></p>
<p><code class="docutils literal notranslate"><span class="pre">Ascend</span></code> <code class="docutils literal notranslate"><span class="pre">GPU</span></code> <code class="docutils literal notranslate"><span class="pre">CPU</span></code></p>
<p><strong>样例：</strong></p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">mindspore</span> <span class="kn">import</span> <span class="n">Tensor</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">=</span> <span class="n">Tensor</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">]))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">x</span><span class="o">.</span><span class="n">searchsorted</span><span class="p">(</span><span class="mi">3</span><span class="p">))</span>
<span class="go">2</span>
</pre></div>
</div>
</dd></dl>

<dl class="method">
<dt id="mindspore.Tensor.shape">
<em class="property">property </em><code class="sig-name descname">shape</code><a class="headerlink" href="#mindspore.Tensor.shape" title="Permalink to this definition">¶</a></dt>
<dd><p>返回Tensor的shape。</p>
</dd></dl>

<dl class="method">
<dt id="mindspore.Tensor.size">
<em class="property">property </em><code class="sig-name descname">size</code><a class="headerlink" href="#mindspore.Tensor.size" title="Permalink to this definition">¶</a></dt>
<dd><p>返回Tensor中的元素总数。</p>
</dd></dl>

<dl class="method">
<dt id="mindspore.Tensor.squeeze">
<code class="sig-name descname">squeeze</code><span class="sig-paren">(</span><em class="sig-param">axis=None</em><span class="sig-paren">)</span><a class="headerlink" href="#mindspore.Tensor.squeeze" title="Permalink to this definition">¶</a></dt>
<dd><p>从Tensor中删除shape为1的维度。</p>
<p><strong>参数：</strong></p>
<p><strong>axis</strong> (Union[None, int, list(int), tuple(int)], optional) - 选择shape中长度为1的条目的子集。如果选择shape条目长度大于1的轴，则报错。默认值为None。</p>
<p><strong>返回：</strong></p>
<p>Tensor，删除了长度为1的维度的全部子集或一个子集。</p>
<p><strong>异常：</strong></p>
<ul class="simple">
<li><p><strong>TypeError</strong> - 输入的参数类型有误。</p></li>
<li><p><strong>ValueError</strong> - 指定维度的shape大于1。</p></li>
</ul>
<p><strong>支持平台：</strong></p>
<p><code class="docutils literal notranslate"><span class="pre">Ascend</span></code> <code class="docutils literal notranslate"><span class="pre">GPU</span></code> <code class="docutils literal notranslate"><span class="pre">CPU</span></code></p>
<p><strong>样例：</strong></p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">mindspore</span> <span class="kn">import</span> <span class="n">Tensor</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">=</span> <span class="n">Tensor</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">ones</span><span class="p">((</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">2</span><span class="p">),</span> <span class="n">dtype</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">float32</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="go">[[[1. 1.]</span>
<span class="go">[1. 1.]]]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">x</span><span class="o">.</span><span class="n">shape</span><span class="p">)</span>
<span class="go">(1, 2, 2)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">y</span> <span class="o">=</span> <span class="n">x</span><span class="o">.</span><span class="n">squeeze</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">y</span><span class="p">)</span>
<span class="go">[[1. 1.]</span>
<span class="go">[1. 1.]]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">y</span><span class="o">.</span><span class="n">shape</span><span class="p">)</span>
<span class="go">(2, 2)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">y</span> <span class="o">=</span> <span class="n">x</span><span class="o">.</span><span class="n">squeeze</span><span class="p">(</span><span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">y</span><span class="p">)</span>
<span class="go">[[1. 1.]</span>
<span class="go">[1. 1.]]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">y</span><span class="o">.</span><span class="n">shape</span><span class="p">)</span>
<span class="go">(2, 2)</span>
</pre></div>
</div>
</dd></dl>

<dl class="method">
<dt id="mindspore.Tensor.std">
<code class="sig-name descname">std</code><span class="sig-paren">(</span><em class="sig-param">axis=None</em>, <em class="sig-param">ddof=0</em>, <em class="sig-param">keepdims=False</em><span class="sig-paren">)</span><a class="headerlink" href="#mindspore.Tensor.std" title="Permalink to this definition">¶</a></dt>
<dd><p>计算指定维度的标准差。
标准差是方差的算术平方根，如：<span class="math notranslate nohighlight">\(std = sqrt(mean(abs(x - x.mean())**2))\)</span> 。</p>
<p>返回标准差。默认情况下计算展开数组的标准差，否则在指定维度上计算。</p>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>不支持NumPy参数 <cite>dtype</cite> 、 <cite>out</cite> 和 <cite>where</cite> 。</p>
</div>
<p><strong>参数：</strong></p>
<ul class="simple">
<li><p><strong>axis</strong> (Union[None, int, tuple(int)]) - 在该维度上计算标准差。默认值：<cite>None</cite> 。如果为 <cite>None</cite> ，则计算展开数组的标准偏差。</p></li>
<li><p><strong>ddof</strong> (int) - δ自由度。计算中使用的除数是 <span class="math notranslate nohighlight">\(N - ddof\)</span> ，其中 <span class="math notranslate nohighlight">\(N\)</span> 表示元素的数量。默认值：0。</p></li>
<li><p><strong>keepdims</strong> - 默认值：<cite>False</cite>。</p></li>
</ul>
<p><strong>返回：</strong></p>
<p>含有标准差数值的Tensor。</p>
<p><strong>支持平台：</strong></p>
<p><code class="docutils literal notranslate"><span class="pre">Ascend</span></code> <code class="docutils literal notranslate"><span class="pre">GPU</span></code> <code class="docutils literal notranslate"><span class="pre">CPU</span></code></p>
<p><strong>样例：</strong></p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">mindspore</span> <span class="kn">import</span> <span class="n">Tensor</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">input_x</span> <span class="o">=</span> <span class="n">Tensor</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">float32</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">output</span> <span class="o">=</span> <span class="n">input_x</span><span class="o">.</span><span class="n">std</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">output</span><span class="p">)</span>
<span class="go">1.118034</span>
</pre></div>
</div>
</dd></dl>

<dl class="method">
<dt id="mindspore.Tensor.strides">
<em class="property">property </em><code class="sig-name descname">strides</code><a class="headerlink" href="#mindspore.Tensor.strides" title="Permalink to this definition">¶</a></dt>
<dd><p>Tensor上每个维度跨度的字节元组。</p>
</dd></dl>

<dl class="method">
<dt id="mindspore.Tensor.sum">
<code class="sig-name descname">sum</code><span class="sig-paren">(</span><em class="sig-param">axis=None</em>, <em class="sig-param">dtype=None</em>, <em class="sig-param">keepdims=False</em>, <em class="sig-param">initial=None</em><span class="sig-paren">)</span><a class="headerlink" href="#mindspore.Tensor.sum" title="Permalink to this definition">¶</a></dt>
<dd><p>返回指定维度上数组元素的总和。</p>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>不支持NumPy参数 <cite>out</cite> 、 <cite>where</cite> 、 <cite>casting</cite> 、 <cite>order</cite> 、 <cite>subok</cite> 、 <cite>signature</cite> 和 <cite>extobj</cite> 。</p>
</div>
<p><strong>参数：</strong></p>
<ul class="simple">
<li><p><strong>axis</strong> (Union[None, int, tuple(int)]) - 指定维度，在该维度方向上进行求和运算。默认值：None。如果参数值为None，会计算输入数组中所有元素的和。如果axis为负数，则从最后一维开始往第一维计算。如果axis为整数元组，会对该元组指定的所有轴方向上的元素进行求和。</p></li>
<li><p><strong>dtype</strong> (<cite>mindspore.dtype</cite>, optional) - 默认值为None。会覆盖输出Tensor的dtype。</p></li>
<li><p><strong>keepdims</strong> (bool) - 如果这个参数为True，被删去的维度保留在结果中，且维度大小设为1。有了这个选项，结果就可以与输入数组进行正确的广播运算。如果设为默认值，那么 <cite>keepdims</cite> 不会被传递给ndarray子类的sum方法。但是任何非默认值都会被传递。如果子类的方法未实现 <cite>keepdims</cite> ，则引发异常。默认值：False。</p></li>
<li><p><strong>initial</strong> (scalar) - 初始化的起始值。默认值：None。</p></li>
</ul>
<p><strong>返回：</strong></p>
<p>Tensor。具有与输入相同shape的Tensor，删除了指定的轴。如果输入Tensor是0维数组，或axis为None时，返回一个标量。</p>
<p><strong>异常：</strong></p>
<ul class="simple">
<li><p><strong>TypeError</strong> - input不是Tensor，<cite>axis</cite> 不是整数或整数元组，<cite>keepdims</cite> 不是整数，或者 <cite>initial</cite> 不是标量。</p></li>
<li><p><strong>ValueError</strong> - 任意轴超出范围或存在重复的轴。</p></li>
</ul>
<p><strong>支持平台：</strong></p>
<p><code class="docutils literal notranslate"><span class="pre">Ascend</span></code> <code class="docutils literal notranslate"><span class="pre">GPU</span></code> <code class="docutils literal notranslate"><span class="pre">CPU</span></code></p>
<p><strong>样例：</strong></p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">mindspore</span> <span class="kn">import</span> <span class="n">Tensor</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">input_x</span> <span class="o">=</span> <span class="n">Tensor</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">])</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">float32</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">input_x</span><span class="o">.</span><span class="n">sum</span><span class="p">())</span>
<span class="go">0.0</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">input_x</span> <span class="o">=</span> <span class="n">Tensor</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">10</span><span class="p">)</span><span class="o">.</span><span class="n">reshape</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">5</span><span class="p">)</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">float32</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">input_x</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">))</span>
<span class="go">[10. 35.]</span>
</pre></div>
</div>
</dd></dl>

<dl class="method">
<dt id="mindspore.Tensor.swapaxes">
<code class="sig-name descname">swapaxes</code><span class="sig-paren">(</span><em class="sig-param">axis1</em>, <em class="sig-param">axis2</em><span class="sig-paren">)</span><a class="headerlink" href="#mindspore.Tensor.swapaxes" title="Permalink to this definition">¶</a></dt>
<dd><p>交换Tensor的两个维度。</p>
<p><strong>参数：</strong></p>
<ul class="simple">
<li><p><strong>axis1</strong> (int) - 第一个维度。</p></li>
<li><p><strong>axis2</strong> (int) - 第二个维度。</p></li>
</ul>
<p><strong>返回：</strong></p>
<p>转化后的Tensor，与输入具有相同的数据类型。</p>
<p><strong>异常：</strong></p>
<ul class="simple">
<li><p><strong>TypeError</strong> - <cite>axis1</cite> 或 <cite>axis2</cite> 不是整数。</p></li>
<li><p><strong>ValueError</strong> - <cite>axis1</cite> 或 <cite>axis2</cite> 不在 <cite>[-ndim, ndim-1]</cite> 范围内。</p></li>
</ul>
<p><strong>支持平台：</strong></p>
<p><code class="docutils literal notranslate"><span class="pre">Ascend</span></code> <code class="docutils literal notranslate"><span class="pre">GPU</span></code> <code class="docutils literal notranslate"><span class="pre">CPU</span></code></p>
<p><strong>样例：</strong></p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">mindspore</span> <span class="kn">import</span> <span class="n">Tensor</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">=</span> <span class="n">Tensor</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">ones</span><span class="p">((</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">4</span><span class="p">),</span> <span class="n">dtype</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">float32</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">output</span> <span class="o">=</span> <span class="n">x</span><span class="o">.</span><span class="n">swapaxes</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">output</span><span class="o">.</span><span class="n">shape</span><span class="p">)</span>
<span class="go">(4,3,2)</span>
</pre></div>
</div>
</dd></dl>

<dl class="method">
<dt id="mindspore.Tensor.take">
<code class="sig-name descname">take</code><span class="sig-paren">(</span><em class="sig-param">indices</em>, <em class="sig-param">axis=None</em>, <em class="sig-param">mode='clip'</em><span class="sig-paren">)</span><a class="headerlink" href="#mindspore.Tensor.take" title="Permalink to this definition">¶</a></dt>
<dd><p>在指定维度上获取Tensor中的元素。</p>
<p><strong>参数：</strong></p>
<ul class="simple">
<li><p><strong>indices</strong> (Tensor) - 待提取的值的shape为 <cite>(Nj…)</cite> 的索引。</p></li>
<li><p><strong>axis</strong> (int, optional) - 在指定维度上选择值。默认情况下，使用展开的输入数组。默认值：None。</p></li>
<li><p><strong>mode</strong> (‘raise’, ‘wrap’, ‘clip’, optional)</p>
<ul>
<li><p>raise：抛出错误。</p></li>
<li><p>wrap：绕接。</p></li>
<li><p>clip：裁剪到范围。 <cite>clip</cite> 模式意味着所有过大的索引都会被在指定轴方向上指向最后一个元素的索引替换。注：这将禁用具有负数的索引。默认值：<cite>clip</cite> 。</p></li>
</ul>
</li>
</ul>
<p><strong>返回：</strong></p>
<p>Tensor，索引的结果。</p>
<p><strong>异常：</strong></p>
<ul class="simple">
<li><p><strong>ValueError</strong> - <cite>axis</cite> 超出范围，或 <cite>mode</cite> 被设置为’raise’、’wrap’和’clip’以外的值。</p></li>
</ul>
<p><strong>支持平台：</strong></p>
<p><code class="docutils literal notranslate"><span class="pre">Ascend</span></code> <code class="docutils literal notranslate"><span class="pre">GPU</span></code> <code class="docutils literal notranslate"><span class="pre">CPU</span></code></p>
<p><strong>样例：</strong></p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">mindspore</span> <span class="kn">import</span> <span class="n">Tensor</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="n">Tensor</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mi">4</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">7</span><span class="p">,</span> <span class="mi">6</span><span class="p">,</span> <span class="mi">8</span><span class="p">]))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">indices</span> <span class="o">=</span> <span class="n">Tensor</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">4</span><span class="p">]))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">output</span> <span class="o">=</span> <span class="n">a</span><span class="o">.</span><span class="n">take</span><span class="p">(</span><span class="n">indices</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">output</span><span class="p">)</span>
<span class="go">[4 3 6]</span>
</pre></div>
</div>
</dd></dl>

<dl class="method">
<dt id="mindspore.Tensor.to_tensor">
<code class="sig-name descname">to_tensor</code><span class="sig-paren">(</span><em class="sig-param">slice_index=None</em>, <em class="sig-param">shape=None</em>, <em class="sig-param">opt_shard_group=None</em><span class="sig-paren">)</span><a class="headerlink" href="#mindspore.Tensor.to_tensor" title="Permalink to this definition">¶</a></dt>
<dd><p>返回init_data()的结果，并获取此Tensor的数据。</p>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>不建议使用 <cite>to_tensor</cite>。请使用 <cite>init_data</cite> 。</p>
</div>
<p><strong>参数：</strong></p>
<ul class="simple">
<li><p><strong>slice_index</strong> (int) - 参数切片的索引。在初始化参数切片的时候使用，保证使用相同切片的设备可以生成相同的Tensor。默认值：None。</p></li>
<li><p><strong>shape</strong> (list[int]) - 切片的shape，在初始化参数切片时使用。默认值：None。</p></li>
<li><p><strong>opt_shard_group</strong> (str) - 优化器分片组，在自动或半自动并行模式下用于获取参数切片的分片。默认值：None。</p></li>
</ul>
<p><strong>返回：</strong></p>
<p>初始化的Tensor。</p>
<p><strong>支持平台：</strong></p>
<p><code class="docutils literal notranslate"><span class="pre">Ascend</span></code> <code class="docutils literal notranslate"><span class="pre">GPU</span></code> <code class="docutils literal notranslate"><span class="pre">CPU</span></code></p>
<p><strong>样例：</strong></p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">mindspore</span> <span class="k">as</span> <span class="nn">ms</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">mindspore.common.initializer</span> <span class="k">as</span> <span class="nn">init</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">=</span> <span class="n">init</span><span class="o">.</span><span class="n">initializer</span><span class="p">(</span><span class="n">init</span><span class="o">.</span><span class="n">Constant</span><span class="p">(</span><span class="mi">1</span><span class="p">),</span> <span class="p">[</span><span class="mi">2</span><span class="p">,</span> <span class="mi">2</span><span class="p">],</span> <span class="n">ms</span><span class="o">.</span><span class="n">float32</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">out</span> <span class="o">=</span> <span class="n">x</span><span class="o">.</span><span class="n">to_tensor</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">out</span><span class="p">)</span>
<span class="go">[[1. 1.]</span>
<span class="go">[1. 1.]]</span>
</pre></div>
</div>
</dd></dl>

<dl class="method">
<dt id="mindspore.Tensor.trace">
<code class="sig-name descname">trace</code><span class="sig-paren">(</span><em class="sig-param">offset=0</em>, <em class="sig-param">axis1=0</em>, <em class="sig-param">axis2=1</em>, <em class="sig-param">dtype=None</em><span class="sig-paren">)</span><a class="headerlink" href="#mindspore.Tensor.trace" title="Permalink to this definition">¶</a></dt>
<dd><p>在Tensor的对角线方向上的总和。</p>
<p><strong>参数：</strong></p>
<ul class="simple">
<li><p><strong>offset</strong> (int, optional) - 对角线与主对角线的偏移。可以是正值或负值。默认为主对角线。</p></li>
<li><p><strong>axis1</strong> (int, optional) - 二维子数组的第一轴，对角线应该从这里开始。默认为第一轴(0)。</p></li>
<li><p><strong>axis2</strong> (int, optional) - 二维子数组的第二轴，对角线应该从这里开始。默认为第二轴。</p></li>
<li><p><strong>dtype</strong> (<cite>mindspore.dtype</cite> , optional) - 默认值为None。覆盖输出Tensor的dtype。</p></li>
</ul>
<p><strong>返回：</strong></p>
<p>Tensor，对角线方向上的总和。</p>
<p><strong>异常：</strong></p>
<p><strong>ValueError</strong> - 输入Tensor的维度少于2。</p>
<p><strong>支持平台：</strong></p>
<p><code class="docutils literal notranslate"><span class="pre">Ascend</span></code> <code class="docutils literal notranslate"><span class="pre">GPU</span></code> <code class="docutils literal notranslate"><span class="pre">CPU</span></code></p>
<p><strong>样例：</strong></p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">mindspore</span> <span class="kn">import</span> <span class="n">Tensor</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">=</span> <span class="n">Tensor</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">eye</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">float32</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">x</span><span class="o">.</span><span class="n">trace</span><span class="p">())</span>
<span class="go">3.0</span>
</pre></div>
</div>
</dd></dl>

<dl class="method">
<dt id="mindspore.Tensor.transpose">
<code class="sig-name descname">transpose</code><span class="sig-paren">(</span><em class="sig-param">*axes</em><span class="sig-paren">)</span><a class="headerlink" href="#mindspore.Tensor.transpose" title="Permalink to this definition">¶</a></dt>
<dd><p>返回被转置后的Tensor。</p>
<ul class="simple">
<li><p>对于一维Tensor，这没有影响，因为转置后的向量是相同的。</p></li>
<li><p>对于二维Tensor，是标准的矩阵转置。</p></li>
<li><p>对于n维Tensor，如果提供了维度，则它们的顺序代表维度的置换方式。</p></li>
</ul>
<p>如果未提供轴，且Tensor.shape等于(i[0], i[1],…i[n-2], i[n-1])，则Tensor.transpose().shape等于(i[n-1], i[n-2], … i[1], i[0])。</p>
<p><strong>参数：</strong></p>
<ul class="simple">
<li><p><strong>axes</strong> (Union[None, tuple(int), list(int), int], optional) - 如果 <cite>axes</cite> 为None或未设置，则该方法将反转维度。如果 <cite>axes</cite> 为tuple(int)或list(int)，则Tensor.transpose()把Tensor转置为新的维度。如果 <cite>axes</cite> 为整数，则此表单仅作为元组/列表表单的备选。</p></li>
</ul>
<p><strong>返回：</strong></p>
<p>Tensor，具有与输入Tensor相同的维度，其中维度被准确的排列。</p>
<p><strong>异常：</strong></p>
<ul class="simple">
<li><p><strong>TypeError</strong> - 输入参数类型有误。</p></li>
<li><p><strong>ValueError</strong> - <cite>axes</cite> 的数量不等于Tensor.ndim。</p></li>
</ul>
<p><strong>支持平台：</strong></p>
<p><code class="docutils literal notranslate"><span class="pre">Ascend</span></code> <code class="docutils literal notranslate"><span class="pre">GPU</span></code> <code class="docutils literal notranslate"><span class="pre">CPU</span></code></p>
<p><strong>样例：</strong></p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">mindspore</span> <span class="kn">import</span> <span class="n">Tensor</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">=</span> <span class="n">Tensor</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">ones</span><span class="p">((</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">),</span> <span class="n">dtype</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">float32</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">=</span> <span class="n">x</span><span class="o">.</span><span class="n">transpose</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">x</span><span class="o">.</span><span class="n">shape</span><span class="p">)</span>
<span class="go">(3, 2, 1)</span>
</pre></div>
</div>
</dd></dl>

<dl class="method">
<dt id="mindspore.Tensor.var">
<code class="sig-name descname">var</code><span class="sig-paren">(</span><em class="sig-param">axis=None</em>, <em class="sig-param">ddof=0</em>, <em class="sig-param">keepdims=False</em><span class="sig-paren">)</span><a class="headerlink" href="#mindspore.Tensor.var" title="Permalink to this definition">¶</a></dt>
<dd><p>在指定维度上的方差。</p>
<p>方差是平均值的平方偏差的平均值，即：<span class="math notranslate nohighlight">\(var = mean(abs(x - x.mean())**2)\)</span> 。</p>
<p>返回方差值。默认情况下计算展开Tensor的方差，否则在指定维度上计算。</p>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>不支持NumPy参数 <cite>dtype</cite> 、 <cite>out</cite> 和 <cite>where</cite> 。</p>
</div>
<p><strong>参数：</strong></p>
<ul class="simple">
<li><p><strong>axis</strong> (Union[None, int, tuple(int)]) - 维度，在指定维度上计算方差。其默认值是展开Tensor的方差。默认值：None。</p></li>
<li><p><strong>ddof</strong> (int) - δ自由度。默认值：0。计算中使用的除数是 <span class="math notranslate nohighlight">\(N - ddof\)</span> ，其中 <span class="math notranslate nohighlight">\(N\)</span> 表示元素的数量。</p></li>
<li><p><strong>keepdims</strong> (bool) - 默认值：False。</p></li>
</ul>
<p><strong>返回：</strong></p>
<p>含有方差值的Tensor。</p>
<p><strong>支持平台：</strong></p>
<p><code class="docutils literal notranslate"><span class="pre">Ascend</span></code> <code class="docutils literal notranslate"><span class="pre">GPU</span></code> <code class="docutils literal notranslate"><span class="pre">CPU</span></code></p>
<p><strong>样例：</strong></p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">mindspore</span> <span class="kn">import</span> <span class="n">Tensor</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">input_x</span> <span class="o">=</span> <span class="n">Tensor</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mf">1.</span><span class="p">,</span> <span class="mf">2.</span><span class="p">,</span> <span class="mf">3.</span><span class="p">,</span> <span class="mf">4.</span><span class="p">],</span> <span class="n">np</span><span class="o">.</span><span class="n">float32</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">output</span> <span class="o">=</span> <span class="n">input_x</span><span class="o">.</span><span class="n">var</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">output</span><span class="p">)</span>
<span class="go">1.25</span>
</pre></div>
</div>
</dd></dl>

<dl class="method">
<dt id="mindspore.Tensor.view">
<code class="sig-name descname">view</code><span class="sig-paren">(</span><em class="sig-param">*shape</em><span class="sig-paren">)</span><a class="headerlink" href="#mindspore.Tensor.view" title="Permalink to this definition">¶</a></dt>
<dd><p>根据输入shape重新创建一个Tensor，与原Tensor数据相同。</p>
<p><strong>参数：</strong></p>
<p><strong>shape</strong> (Union[tuple(int), int]) - 输出Tensor的维度。</p>
<p><strong>返回：</strong></p>
<p>Tensor，具有与输入shape相同的维度。</p>
<p><strong>样例：</strong></p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">mindspore</span> <span class="kn">import</span> <span class="n">Tensor</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="n">Tensor</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">],</span> <span class="p">[</span><span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">]],</span> <span class="n">dtype</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">float32</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">output</span> <span class="o">=</span> <span class="n">a</span><span class="o">.</span><span class="n">view</span><span class="p">((</span><span class="mi">3</span><span class="p">,</span> <span class="mi">2</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">output</span><span class="p">)</span>
<span class="go">[[1. 2.]</span>
<span class="go">[3. 2.]</span>
<span class="go">[3. 4.]]</span>
</pre></div>
</div>
</dd></dl>

</dd></dl>

</div>


           </div>
           
          </div>
          <footer>
    <div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
        <a href="mindspore.RowTensor.html" class="btn btn-neutral float-right" title="mindspore.RowTensor" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right" aria-hidden="true"></span></a>
        <a href="../mindspore.html" class="btn btn-neutral float-left" title="mindspore" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left" aria-hidden="true"></span> Previous</a>
    </div>

  <hr/>

  <div role="contentinfo">
    <p>
        &#169; Copyright 2021, MindSpore.

    </p>
  </div>
    
    
    
    Built with <a href="https://www.sphinx-doc.org/">Sphinx</a> using a
    
    <a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a>
    
    provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>
        </div>
      </div>

    </section>

  </div>
  

  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
   

</body>
</html>